package org.mics.hikariMonitor.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.mics.hikariMonitor.entity.HikariMonitorData;
import org.mics.hikariMonitor.repository.HikariMonitorDataRepository;
import org.mics.hikariMonitor.service.HikariMonitorDataService;
import org.mics.hikariMonitor.vo.MonitorDataVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.HikariPoolMXBean;

/**
 * hikari监听服务实现
 * @author mics
 * @date 2022-7-21
 * @version  1.0
 */
@Service
public class HikariMonitorDataServiceImpl implements HikariMonitorDataService{
	@Value("${server.port}")
	private String serverPort;
	@Resource
	private HikariDataSource hikariDataSource;
	@Resource
	private HikariMonitorDataRepository hikariMonitorDataRepository;

	/**
	 * 每隔五秒中进行一次监听数据写入
	 * @author mics
	 * @date 2022-7-22
	 * @version  1.0
	 */
    @Transactional
    @Scheduled(fixedRate =5000)
	public void produceData() {
        HikariPoolMXBean mx = hikariDataSource.getHikariPoolMXBean();
        HikariMonitorData hikariMonitorData = new HikariMonitorData();
        hikariMonitorData.setServerPort(serverPort);
        hikariMonitorData.setTotalConnection(mx.getTotalConnections());
        hikariMonitorData.setActiveConnection(mx.getActiveConnections());
        hikariMonitorData.setIdleConnection(mx.getIdleConnections());
        hikariMonitorData.setThreadAwaitingConnection(mx.getThreadsAwaitingConnection());
        hikariMonitorDataRepository.save(hikariMonitorData);
	}
    
    /**
     * 每隔一分钟删除24小时之前的数据
     * @author mics
     * @date 2022-7-22
     * @version  1.0
     */
    @Transactional
    @Scheduled(fixedDelay = 60000)
    public void removeOverdueData() {
    	hikariMonitorDataRepository.removeOverdueData();
    }

	@Override
	public List<MonitorDataVO> listByHour() {
		List<Map<String,Object>> results =  hikariMonitorDataRepository.listByHour(); 
		List<MonitorDataVO> monitorDataVOs = new ArrayList<MonitorDataVO>();
		for(Map<String,Object> result:results ) {
			MonitorDataVO monitorDataVO = new MonitorDataVO();
			monitorDataVO.setDateStr(String.valueOf(result.get("dateStr")));
			monitorDataVO.setActiveConnection(new BigDecimal(String.valueOf(result.get("activeConnection"))));
			monitorDataVO.setIdleConnection(new BigDecimal(String.valueOf(result.get("idleConnection"))));
			monitorDataVO.setThreadAwaitingConnection(new BigDecimal(String.valueOf(result.get("threadAwaitingConnection"))));
			monitorDataVO.setTotalConnection(new BigDecimal(String.valueOf(result.get("totalConnection"))));
			monitorDataVOs.add(monitorDataVO);
		}
		return monitorDataVOs;
	}

	@Override
	public List<MonitorDataVO> listByMinite(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH");
		String datestr = simpleDateFormat.format(date);
		List<Map<String,Object>> results =  hikariMonitorDataRepository.listByMinite(datestr);
		List<MonitorDataVO> monitorDataVOs = new ArrayList<MonitorDataVO>();
		for(Map<String,Object> result:results ) {
			MonitorDataVO monitorDataVO = new MonitorDataVO();
			monitorDataVO.setDateStr(String.valueOf(result.get("dateStr")));
			monitorDataVO.setActiveConnection(new BigDecimal(String.valueOf(result.get("activeConnection"))));
			monitorDataVO.setIdleConnection(new BigDecimal(String.valueOf(result.get("idleConnection"))));
			monitorDataVO.setThreadAwaitingConnection(new BigDecimal(String.valueOf(result.get("threadAwaitingConnection"))));
			monitorDataVO.setTotalConnection(new BigDecimal(String.valueOf(result.get("totalConnection"))));
			monitorDataVOs.add(monitorDataVO);
		}
		return monitorDataVOs;
	}
    
}
